<?php
declare (strict_types=1);

namespace app\controller;

use app\BaseController;
use app\traits\JsonResponse;
use think\exception\ValidateException;
use think\Request;
use app\model\Topic as TopicModel;

class Topic extends BaseController {
    use JsonResponse;

    protected $middleware = [
        'auth_jwt' => ['only' => ['save', 'update', 'delete']],
    ];

    public function index() {
       $topics = TopicModel::order('sort','sort')->with(['user','cat'])->paginate();
       return $this->jsonSuccess($topics);
    }
    public function save(Request $request,TopicModel $topic) {
        try {
            validate([
                'title|帖子名称' => 'require|max:255',
                'content|帖子内容' => 'require|max:20000',
                'sort|排序' => 'integer|egt:0',
                'is_hot|排序' => 'integer|egt:0',
                'cat_id|所属大类ID' => 'require|integer|egt:0'
            ])->batch()->check($request->post());
            $topic->title = $request->post('title');
            $topic->content = $request->post('content');
            $topic->user_id = $this->user_id;
            $topic->cat_id = $request->post('cat_id');
            if(isSuperAdmin($this->user_id)){
                $topic->is_hot = $request->post('is_hot') ?? 1;
                $topic->sort = $request->post('sort') ?? 100;
            }
            $topic->save();
            return $this->jsonSuccess($topic, '帖子添加成功', 201);
        } catch (ValidateException $e) {
            return $this->jsonFail($e->getError(), '输入信息不合法', 422);
        }
    }
    public function read($id) {
        $topic = TopicModel::with(['user','cat'])->findOrEmpty($id);
        if(!$topic->isEmpty()){
            return $this->jsonSuccess($topic);
        }else{
            return $this->jsonFail();
        }
    }
    public function update(Request $request, $id) {
        try {
            validate([
                'title|帖子名称' => 'require|max:255',
                'content|帖子内容' => 'require|max:20000',
                'sort|排序' => 'integer|egt:0',
                'is_hot|排序' => 'integer|egt:0',
                'cat_id|所属大类ID' => 'require|integer|integer|egt:0'
            ])->batch()->check($request->post());
            $topic = TopicModel::findOrEmpty($id);
            if(!$topic->isEmpty()){
                if($topic->user_id == $this->user_id || isSuperAdmin($this->user_id)){
                    $topic->title = $request->post('title');
                    $topic->content = $request->post('content');
                    $topic->cat_id = $request->post('cat_id');
                    if(isSuperAdmin($this->user_id)){
                        $topic->is_hot = $request->post('is_hot') ?? $topic->is_hot;
                        $topic->sort = $request->post('sort') ??  $topic->sort;
                    }
                    $topic->save();
                    return $this->jsonSuccess($topic, '帖子修改成功');
                } else{
                   return  $this->jsonFail(message:'非法操作');
                }
            }else{
                return $this->jsonFail();
            }

        } catch (ValidateException $e) {
            return $this->jsonFail($e->getError(), '输入信息不合法', 422);
        }
    }
    public function delete($id) {
        $topic = TopicModel::findOrEmpty($id);
        if(!$topic->isEmpty()){
            if($this->user_id == $topic->user_id || isSuperAdmin($this->user_id)){
                $topic->delete();
                return $this->jsonSuccess(message: '帖子修改成功');
            }else{
                return $this->jsonFail('非法操作');
            }
        }else{
            return $this->jsonFail();
        }
    }
}
